package 剑指offer;

import java.util.LinkedList;
import java.util.Stack;

/**
 * @Description:
 * @Author: Administrator
 * @Date: 2020-05-03
 */
public class 二叉搜索树与双向链表 {

    public static TreeNode Convert(TreeNode pRootOfTree) {
        //通过中序遍历
        //通过中序遍历
        if (pRootOfTree == null) {
            return pRootOfTree;
        }
        LinkedList<TreeNode> stack = new LinkedList();
        TreeNode idol = pRootOfTree;
        stack.push(idol);
        boolean isHead = true;
        TreeNode head = null;
        TreeNode preNode = null;
        TreeNode parent = null;
        while (!stack.isEmpty()) {
            while (idol != null && idol.left != null) {
                stack.addLast(idol.left);
                idol = idol.left;
            }
            idol = stack.pollLast();
            System.out.println(idol.val);
            if (idol.right != null) {
                stack.addLast(idol.right);
            }
            if (isHead) {
                head = idol;
                preNode = head;
                isHead = false;
            } else {
                idol.left = preNode;
                preNode.right = idol;
                preNode = idol;

            }
            idol = idol.right;

        }
        return head;
    }

    public static void middleTraverse(TreeNode root) {
        //通过中序遍历
        Stack<TreeNode> stack = new Stack();
        TreeNode idol = root;
        boolean isHead = true;
        TreeNode head = null;
        TreeNode preNode = null;
        while (true) {
            while (idol != null) {
                stack.push(idol);
                idol = idol.left;
            }
            if (stack.isEmpty()) {
                break;
            }
            idol = stack.pop();
            System.out.println(idol.val);
            idol = idol.right;

        }

    }

    public static void printTree(TreeNode tree) {
        if (tree != null) {
            //System.out.print(tree.val+" ");//先序遍历
            printTree(tree.left);
            System.out.print(tree.val + " ");//MiddleMy
            printTree(tree.right);
            //System.out.print(tree.val+" ");//后序遍历
        }
    }

    public static void printLink(TreeNode tree) {

        while (tree != null) {

            if (tree.left != null) {
                System.out.print(tree.left.val + "\t");
            }
            if (tree.right != null) {
                System.out.print(tree.right.val + "\t");
            }
            tree = tree.right;
        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TreeNode root = new TreeNode(8);
        root.left = new TreeNode(6);
        root.right = new TreeNode(10);
        root.left.left = new TreeNode(5);
        root.left.right = new TreeNode(7);
        root.right.left = new TreeNode(9);
        root.right.right = new TreeNode(11);
//        printTree(root);
        middleTraverse(root);
        System.out.println();
//        printLink(Convert(root));

    }

}
